package edu.must.uh.servlet;

import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import edu.must.uh.bean.DeployQueryResultBean;
import edu.must.uh.impl.BaseUserExImpl;
import edu.must.uh.impl.DepartmentImpl;
import edu.must.uh.impl.UserImpl;
import edu.must.uh.util.R;
import edu.must.uh.util.ServletUtils;
import edu.must.uh.util.Tools;
import edu.must.uh.util.ToolsOfString;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
    private static final long serialVersionUID = 12197442526341123L;
    private static Logger logger = Logger.getLogger(UserServlet.class);  
    UserImpl userImpl = new UserImpl();
    
    public Object getList(HttpServletRequest request, HttpServletResponse response) throws Exception {      
     
    	userImpl._PageBean = super.pageBean;
        Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
        DeployQueryResultBean resList = userImpl.getLimitlist(searchcondition, " ORDER BY DepartmentId, SortCode, Account ");
                 
        return R.succ(resList.getResultList(),resList.getTotal());         
    }
    
    public Object getUserSelect(HttpServletRequest request, HttpServletResponse response) throws Exception {      
        
    	BaseUserExImpl baseUserExImpl = new BaseUserExImpl();    	
    	List<Map<String, Object>> resList = baseUserExImpl.getUserList();                 
        return R.succ(resList);         
    }
    
    public Object getOneData(HttpServletRequest request, HttpServletResponse response) throws Exception {      
        
    	String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
    	String sqlString = "SELECT  UserId, Account,RealName,Gender,Mobile,Email,DepartmentId,RoleId,DutyId,PostId,RFullName,DRFullName,PRFullName,DFullName,SortCode FROM v_base_user Where DeleteMark= 0  ";
    	sqlString = sqlString + " AND UserId = '"+keyValue+"'";
        Map<String,Object>  map = userImpl.getMap(sqlString);                 
        return R.succ(map);         
    }
    
    public Object delete(HttpServletRequest request, HttpServletResponse response) throws Exception {      
        
    	
        String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
        Integer resInteger = userImpl.delete(keyValue);
        return R.succ(resInteger);         
    }
    
  public Object save(HttpServletRequest request, HttpServletResponse response) throws Exception {  
      
	    String sql = "";
		Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
        String UserId =ToolsOfString.NulltoEmpty(searchcondition.get("UserId"));  
        String RealName =ToolsOfString.NulltoEmpty(searchcondition.get("RealName"));
        RealName =  java.net.URLDecoder.decode(RealName, "UTF-8");
        String Account =ToolsOfString.NulltoEmpty(searchcondition.get("Account"));
        String Gender =ToolsOfString.NulltoEmpty(searchcondition.get("Gender"));
        String Mobile =ToolsOfString.NulltoEmpty(searchcondition.get("Mobile"));
        String DepartmentId =ToolsOfString.NulltoEmpty(searchcondition.get("DepartmentId"));
        String RoleId =ToolsOfString.NulltoEmpty(searchcondition.get("RoleId"));
        String DutyId =ToolsOfString.NulltoEmpty(searchcondition.get("DutyId"));
        String PostId =ToolsOfString.NulltoEmpty(searchcondition.get("PostId"));
        String Email =ToolsOfString.NulltoEmpty(searchcondition.get("Email"));
        String SortCode =ToolsOfString.NulltoEmpty(searchcondition.get("SortCode"));     
        if(Tools.notEmpty(UserId)) {
        	sql = "UPDATE base_user  SET RealName = '"+RealName+"',Account = '"+Account+"',Gender = '" +Gender+"',Mobile = '" +Mobile+"',DepartmentId = '" +DepartmentId+"',RoleId = '" +RoleId+"',DutyId = '" +DutyId+"',PostId = '" +PostId+"'";
        	sql = sql + " ,Email = '"+Email+"',SortCode = '"+SortCode+"'  WHERE UserId ='"+UserId+"'"; 
        }else {
        	UserId = Tools.getUUID32();
        	sql = "INSERT INTO base_user(UserId,RealName,Account,Gender,Mobile,DepartmentId,RoleId,DutyId,PostId,Email,SortCode)VALUES";
        	sql = sql + " ('"+UserId+"','"+RealName+"','"+Account+"','"+Gender+"','"+Mobile+"','"+DepartmentId+"','"+RoleId+"','"+DutyId+"','"+PostId+"','"+Email+"','"+SortCode+"')"; 
        }
        Integer resInteger = userImpl.save(sql);
        if(resInteger == 1) {
        	   return R.succ(); 
        }
        return R.fail();
       
    }
    
    
    
   
}
